package com.zj.executor;

import com.zj.mapping.MappedStatement;
import com.zj.sqlSession.ResultHandler;
import com.zj.transaction.Transaction;
import com.zj.utils.Configuration;

import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

public abstract class BaseExecutor implements Executor{
    protected Executor wrapper;
    protected Configuration configuration;
    protected Transaction transaction;

    protected BaseExecutor(Configuration configuration, Transaction transaction) {
        this.transaction = transaction;
        this.configuration = configuration;
    }

    @Override
    public int update(MappedStatement ms, Object parameter) throws SQLException {
        return doUpdate(ms, parameter);
    }

    @Override
    public void commit(boolean required) throws SQLException {
        if (required) {
            transaction.commit();
        }
    }

    @Override
    public void rollback(boolean required) throws SQLException {
        if (required) {
            transaction.rollback();
        }
    }

    @Override
    public Transaction getTransaction() {
        return transaction;
    }

    @Override
    public void close(boolean forceRollback) {
        try {
            try {
                rollback(forceRollback);
            } finally {
                if (transaction != null) transaction.close();
            }
        } catch (SQLException e) {
            // Ignore.  There's nothing that can be done at this point.
        } finally {
            transaction = null;
        }
    }
    protected abstract int doUpdate(MappedStatement ms, Object parameter)throws SQLException;

    protected abstract <E> List<E> doQuery(MappedStatement ms, Object parameter)
            throws SQLException;

    public <E> List<E> query(MappedStatement ms, Object parameter) throws SQLException {
    List<E> list;
    try {
        list = doQuery(ms, parameter);
    }finally {
        ;
    }
    return list;
}
}
